/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: connect_mysql.c
> Author: lv1
> Website:www.lv1.org
> Created Time: 2024年08月02日 星期五 10时30分31秒
***************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
typedef struct stu{
	int code;
	char name[20];
	int age;
}STU;

int main(int argc, const char* argv[])
{
	MYSQL *connect = NULL;
	MYSQL *res = NULL;
	char insert_sql[100]={0};
	char delete_sql[100]={0};
	//连接数据库
	connect = mysql_init(NULL);
	res = mysql_real_connect(connect,"localhost","username","1","cproject",0,NULL,0);
	if(res == NULL){
		printf("%s",mysql_error(connect));
		exit(-1);
	}
	printf("连接mysql成功！\n");
	
	/*
	//插入数据
	STU k;
	printf("请输入编号code:");
	scanf("%d",&k.code);
	printf("请输入姓名：");
	scanf("%s",k.name);
	printf("请输入年龄：");
	scanf("%d",&k.age);
	sprintf(insert_sql,"insert into student values(%d,'%s',%d)",k.code,k.name,k.age);
	int ret = 0;
	ret = mysql_query(connect,insert_sql);
	if(ret != 0)
	{
		printf("%s",mysql_error(connect));
	}
	printf("插入数据成功");
	*/

	/*
	//删除学员信息
	printf("删除code为多少的学员信息：");
	int temp = 0;
	scanf("%d",&temp);
	sprintf(delete_sql,"delete from student where code = %d",temp);
	int ret1 = 0;
	ret1 = mysql_query(connect,delete_sql);
	if(ret1 != 0)
	{
		printf("%s",mysql_error(connect));
	}
	printf("删除数据成功！");
	*/	

	//修改学员信息
	char update_sql[100]={0};
	printf("输入编号code，修改年龄为50:");
	int code = 0;
	scanf("%d",&code);
	sprintf(update_sql,"update student set age = 50 where code = %d",code);
	int ret2 = 0;
	ret2 = mysql_query(connect,update_sql);
	if(ret2 != 0){
		printf("%s",mysql_error(connect));
	}
	printf("数据修改成功！");









    return 0;
}
